<template>
	<view>
		<scroll-view  class="baseview basebottom" scroll-y="true">
		
		  <!--for数组中,item是数组项,index是数组下标-->
				<block v-for="(item,index) in addresses" :key="index" >
		            <view class="content flex-wrap flex-direction-row">
		
		                <!--可能点中的是text区域，需要给text设置data-index-->
		                <view  class="flex-wrap flex-direction-col textview" @click="selectAddress(index)">
		                    <text  class = "title">{{item.name}}   {{item.phone}}</text>
		                    <text  class="address-detail">{{item.detail}}</text>
		                    <text  class="address-detail">{{item.note}}</text>
		                </view>
		                <text class="default" v-if="index==0">默认地址</text>
		                <image src="../../../static/edit.png" class="edit-button" @click="editAddress(index)" />
		            </view>
				</block>
		
		</scroll-view>
		
		
		<!--可能点中的是text区域，为了使传输的index是-1，需要给text设置data-index="-1"-->
		<view class="cart plus" @click="editAddress(-1)" >
		    <text data-index="-1" class="txt">增加地址</text>
		</view> 
		

	</view>
</template>

<script>
	export default {
		data() {
			return {
				addresses: []
			}
		},
		onShow() {
			  let that = this
			    uni.getStorage({
			      key: 'address',
			      success: function (res) {
			        that.addresses= res.data
			      },
			      fail: function () {
			        that.setAddressStorageSync()
			      }
			    })
		},
		methods: {
			  setAddressStorageSync: function () {
			    try {
			      uni.setStorageSync('address', this.addresses)
			    } catch (e) {
					this.$uniApi.tipMsg('保存失败')
			    }
			  },
			
			  selectAddress: function (i) {
			    let selcectIndex = parseInt(i)
			    let address = this.addresses[selcectIndex]
			    this.addresses.splice(selcectIndex, 1)
			    this.addresses.unshift(address)  //把选中的address放到数组的第一个位置
			    // this.setData({
			    //   addresses: this.data.addresses
			    // })
			    this.setAddressStorageSync()
			  },
			
			  editAddress: function (i) {
			
			    let selcectIndex = parseInt(i)
			    uni.navigateTo({
			      url: '../addredit/addredit?index=' + selcectIndex,
			    })
			
			  }
		}
	}
</script>

<style>

.address-detail{
    margin-top: 15rpx;
    font-size: 30rpx;
    color: gray;
}

.title{
    font-size: 32rpx;
}

.default{
    background: #1ea1aa;
    width: 120rpx;
    height: 45rpx;
    margin-top: 9.5rpx;
    margin-right: 10px;
    font-size: 22rpx;
    line-height: 45rpx;
    text-align: center;
    color: white;
    border-radius: 2px;
}

.textview{
    flex: 1;
}

.edit-button{
    height: 60rpx;
    width: 60rpx;  
    margin-right: 10px;
}

.plus{
    background-color: #00b08b;
    color: white;
    height: 100rpx;
}

.plus .txt{
    width: 100%;
    line-height: 100rpx;
    text-align: center;
}
</style>
